<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入课程名称"
          size="small"
          v-model="queryParams.courseName"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          size="mini"
          type="warning"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="watchList" v-loading="loading">
      <el-table-column align="center" label="课程名称" prop="courseName"/>
      <el-table-column align="center" label="当前观看至" prop="watchProgress"/>
      <el-table-column :formatter="typeFormat" align="center" label="是否学完" prop="isComplate"/>
      <el-table-column align="center" label="观看次数" prop="watchNum"/>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />
  </div>
</template>

<script>
  import {recordListWatch, exportWatchRecord} from "@/api/system/watch";

  export default {
    name: "Watch",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 表格数据
        watchList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        typeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          courseName: undefined,
          userId: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      const userId = this.$route.params && this.$route.params.userId;
      this.getList(userId);
      this.getDicts("sys_course_watch").then(response => {
        this.typeOptions = response.data;
      });
    },
    methods: {
      /** 查询列表 */
      getList(userId) {
        this.loading = true;
        this.queryParams.userId = userId;
        recordListWatch(this.queryParams).then(response => {
          this.watchList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.isComplate);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          courseName: undefined,
          userId: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        const userId = this.$route.params && this.$route.params.userId;
        this.queryParams.pageNum = 1;
        this.getList(userId);
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportWatchRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
